package com.example.service.impl;

import com.example.mapper.ClazzMapper;
import com.example.pojo.*;
import com.example.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 基于pageHelper分页查询
     * @param
     * @param
     * @return
     */
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());

        //执行查询
        List<Clazz> clazzList = clazzMapper.page(clazzQueryParam);
        //解析查询结果，封装
        Page<Clazz> p = (Page<Clazz>) clazzList;

        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }

    /**
     * 根据ID删除班级信息
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    /**
     * 新增班级信息
     * @param clazz
     */
    @Override
    public void save(Clazz clazz) {
        //1.补全基础属性
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //2.调用mapper层新增
        clazzMapper.save(clazz);
    }

    /**
     * 根据ID查询班级信息
     * @param id
     * @return
     */
    @Override
    public Clazz findById(Integer id) {
        return clazzMapper.findById(id);
    }

    /**
     * 修改班级信息
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
       //1.补全基础属性
       clazz.setUpdateTime(LocalDateTime.now());
       //2.调用mapper层修改
       clazzMapper.update(clazz);
    }

    /**
     * 查询所有班级信息
     * @return
     */
    @Override
    public List<Clazz> list() {
        return clazzMapper.list();
    }
}
